import { formatTime } from '../../utils/util'
Page({
    data: {
        loading: true,
        info: null,
        openPay: false,
        payLoading: false,
        logistics: [], //物流信息
        activeIcon: '', //当前物流状态的图标
        scrollIntoLogistics: false
    },
    onLoad: function(options) {
        this.setData({
            navInfo: getApp().globalData.navInfo,
            scrollIntoLogistics: options.scrollIntoLogistics === 'true'
        })
        this.getDetail(options.order_id)
    },
    /**获取订单详情 */
    getDetail(order_id) {
        this.request({
            url: '/api/order/detail',
            loadingTip: '加载中',
            data: { order_id }
        }, res => {
            /**计算取消时间 */
            let createTime = new Date(res.data.created_at)
            let cancel_time = new Date(createTime.setSeconds(createTime.getSeconds() + 170))
            res.data.cancel_time = formatTime(cancel_time)

            if (res.data.order_status === -1) res.data.order_status = 6
            this.setData({ info: res.data, loading: false })

            /**待收货和已完成时查询物流单号 */
            if (res.data.shipping_type !== 0 && (res.data.order_status === 4 || res.data.order_status === 8)) {
                res.data.express_number ? this.queryLogistics(res.data.order_id, res.data.express_number) : this.scrollIntoLogistics()
            }
        })
    },
    /**取消订单 */
    cancel() {
        wx.$dialog.confirm({
            title: '提示',
            message: `您确定取消该订单吗？`,
            theme: 'round-button',
            beforeClose: (action) => new Promise((resolve) => {
                if (action === 'confirm') {
                    return this.request({
                        url: '/api/order/cancel',
                        method: 'POST',
                        data: {
                            order_id: this.data.info.order.order_id
                        }
                    }, () => {
                        resolve(true)
                        wx.showToast({
                            title: '取消成功',
                            icon: 'success',
                            mask: true
                        })
                        this.oprration_success()
                    }, () => {
                        resolve()
                    })
                }
                resolve(true)
            })
        })
    },
    /**点击立即支付 */
    pay() {
        this.setData({ payLoading: true })
        this.request({
            url: '/api/payment/order',
            method: 'POST',
            data: {
                code: 'wxpay',
                order_id: this.data.info.order.order_id
            }
        }, ({ data }) => {
            wx.requestPayment({
                timeStamp: data.pay_data.timeStamp,
                nonceStr: data.pay_data.nonceStr,
                package: data.pay_data.package,
                signType: data.pay_data.signType,
                paySign: data.pay_data.paySign,
                success: () => {
                    wx.showToast({
                        title: '支付成功',
                        icon: 'success',
                        mask: true
                    })
                    this.oprration_success()
                    this.setData({ payLoading: false, openPay: false })
                },
                fail: () => {
                    wx.showToast({
                        title: '支付失败',
                        icon: 'error'
                    })
                    this.setData({ payLoading: false })
                }
            })
        }, () => {
            wx.showToast({
                title: '支付失败',
                icon: 'error'
            })
            this.setData({ payLoading: false })
        })
    },
    /**点击确认收货 */
    receiving() {
        wx.$dialog.confirm({
            title: '提示',
            message: `您确定已收到货吗？`,
            theme: 'round-button',
            beforeClose: (action) => new Promise((resolve) => {
                if (action === 'confirm') {
                    return this.request({
                        url: '/api/order/finished',
                        method: 'POST',
                        data: {
                            order_id: this.data.info.order.order_id
                        }
                    }, () => {
                        resolve(true)
                        wx.showToast({
                            title: '收货成功',
                            icon: 'success',
                            mask: true
                        })
                        this.oprration_success()
                    }, () => {
                        resolve()
                    })
                }
                resolve(true)
            })
        })
    },
    oprration_success() {
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.emit('splice')
        this.delay(() => {
            this.getDetail(this.data.info.order.order_id)
        })
    },
    /**点击退款 */
    refund(e) {
        const { supplier_index, good_index } = e.currentTarget.dataset
        const { info } = this.data
        let data = {
            supplierInfo: {
                logo: info.supplier_list[supplier_index].logo,
                title: info.supplier_list[supplier_index].title,
                supplier_id: info.supplier_list[supplier_index].supplier_id,
            },
            goodInfo: info.supplier_list[supplier_index].product_list[good_index]
        }
        wx.navigateTo({
            url: '/pages/refund/index',
            events: { //退款页操作成功，此页商品退款状态变更
                markRefund: () => {
                    this.setData({
                        ['info.supplier_list[' + supplier_index + '].product_list[' + good_index + '].complaint_status']: 1
                    })
                }
            },
            success: res => {
                res.eventChannel.emit('acceptDataFromOpenerPage', { data })
            }
        })
    },
    /**点击评价 */
    evaluate(e) {
        const { supplier_index, good_index } = e.currentTarget.dataset
        const { info } = this.data
        let data = {
            supplierInfo: {
                logo: info.supplier_list[supplier_index].logo,
                title: info.supplier_list[supplier_index].title,
                supplier_id: info.supplier_list[supplier_index].supplier_id,
            },
            goodInfo: info.supplier_list[supplier_index].product_list[good_index]
        }
        wx.navigateTo({
            url: '/pages/evaluate/index',
            events: { //评价成功，此页商品状态变更
                markEvaluate: () => {
                    this.setData({
                        ['info.supplier_list[' + supplier_index + '].product_list[' + good_index + '].comment_status']: 1
                    })
                }
            },
            success: res => {
                res.eventChannel.emit('acceptDataFromOpenerPage', { data })
            }
        })
    },
    /**复制物流单号 */
    clipboard() {
        if (this.data.info.express_number) {
            wx.setClipboardData({
                data: this.data.info.express_number
            })
        }
    },
    /**查询物流信息 */
    queryLogistics(order_id, express_number) {
        this.request({
            url: '/api/order/kdData',
            data: {
                order_id,
                express_number
            }
        }, res => {
            if (!res.data.kd_data) return
            let steps = res.data.kd_data.map(item => {
                return {
                    text: item.ftime,
                    desc: item.context
                }
            })
            let activeIcon = 'underway'

            if (res.data.state == 3) {
                activeIcon = 'checked'
            } else if (res.data.state == 5) {
                activeIcon = 'manager'
            }

            this.setData({
                logistics: steps,
                activeIcon
            })
            this.scrollIntoLogistics()
        })
    },
})